package com.example.nationmobile;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

import com.coe3.data.AllIntent;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;


import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class NewsDetailActivity extends Activity implements OnClickListener {

	 private ImageLoader imageLoader = ImageLoader.getInstance();
	 private DisplayImageOptions options;
	 
	 private TextView txt_title,txt_detail,txt_des,txt_byline,txt_topic;
	 private ImageView img_photo,img_share,img_save;
	
	 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		Log.e("evt", "====================onCreate====================");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_news_detail);
		
		//========================img==========================
		 options = new DisplayImageOptions.Builder()
			.showStubImage(R.drawable.thumb)
			.showImageForEmptyUri(R.drawable.thumb)
			.showImageOnFail(R.drawable.thumb)
			.cacheInMemory()
			.cacheOnDisc()
			.displayer(new RoundedBitmapDisplayer(0))
			.build();
		initImageLoader(NewsDetailActivity.this);
		
		
		
		Log.e("NT", ""+Control.news_description+"");
		Log.e("NT", ""+Control.news_detail+"");
		Log.e("NT", ""+Control.news_photo+"");
		Log.e("NT", ""+Control.news_title+"");
		
		
		SetContent();
		
		
	}
	
	
	private void SetContent(){
		
		 txt_title=(TextView)findViewById(R.id.txt_title);
		 txt_detail=(TextView)findViewById(R.id.txt_detail);
		 img_photo=(ImageView)findViewById(R.id.img_photo);
		 txt_des=(TextView)findViewById(R.id.txt_des);
		 txt_byline=(TextView)findViewById(R.id.txt_byline);
		 txt_topic=(TextView)findViewById(R.id.txt_topic);
		 
		 
		 img_share=(ImageView)findViewById(R.id.img_share);
		 img_share.setOnClickListener(this);
		 img_save=(ImageView)findViewById(R.id.img_save);
		 img_save.setOnClickListener(this);
		 
		 txt_title.setText(Control.news_title);
		 txt_detail.setText(Html.fromHtml(Control.news_detail));
	     imageLoader.displayImage(Control.news_photo, img_photo, options, null);
	     txt_des.setText(Control.news_description);
	     txt_byline.setText(Control.news_byline);
	     txt_topic.setText(Control.Tab_select[Control.current_tab]);
	}
	
	
	
	
	 public static void initImageLoader(Context context) {
	  		// This configuration tuning is custom. You can tune every option, you may tune some of them, 
	  		// or you can create default configuration by
	  		//  ImageLoaderConfiguration.createDefault(this);
	  		// method.
	  		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
			  		.memoryCacheExtraOptions(600, 800) // default = device screen dimensions
			        .threadPoolSize(3) // default
			        .threadPriority(Thread.NORM_PRIORITY - 1) // default
			        .tasksProcessingOrder(QueueProcessingType.FIFO) // default
			        .denyCacheImageMultipleSizesInMemory()
			        .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
			        .memoryCacheSize(2 * 1024 * 1024)
			        .discCacheSize(50 * 1024 * 1024)
			        .discCacheFileCount(100)
			        .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
			        .imageDownloader(new BaseImageDownloader(context)) // default
			        .imageDecoder(new BaseImageDecoder()) // default
			       // .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
			        .build();
	  		// Initialize ImageLoader with configuration.
	  		ImageLoader.getInstance().init(config);
	  	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==R.id.img_share)
		{
			AllIntent intent=new AllIntent(NewsDetailActivity.this);
			intent.ShareDG(Control.news_link,Control.news_description,Control.news_title, "");
			
		}
		
		if(v.getId()==R.id.img_save)
		{

			SaveFile();
			ReadFile();
			
		}
		
	}

	
	
	private String PathFile="/mnt/sdcard/Android/data/com.example.nationmobile/cache/Favotite/";
	
	private void SaveFile()
	{
		
		
		
		
		
		
	// create a File object for the parent directory
		File wallpaperDirectory = new File("/mnt/sdcard/Android/data/com.example.nationmobile/cache/Favotite");
		// have the object build the directory structure, if needed.
		wallpaperDirectory.mkdirs();
		
		
		FileWriter fw;
		try {
			fw = new FileWriter(PathFile+Control.news_title);
		
		  BufferedWriter out = new BufferedWriter(fw);
		  out.write(Control.news_title);out.newLine();//0
		  out.write(Control.news_detail);out.newLine();//1
		  out.write(Control.news_description);out.newLine();//2
		  out.write(Control.news_photo);out.newLine();//3
		  out.write(Control.news_thumbnail);out.newLine();//4
		  out.write(Control.news_link);out.newLine();//5
		  
		  out.close();
		  
		  
		  Toast.makeText(
					this,
					"Favorites",
					Toast.LENGTH_LONG).show();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private void ReadFile()
	{
		try {
			 FileInputStream fs = new FileInputStream(PathFile+Control.news_title);  
			  DataInputStream ds = new DataInputStream(fs);
			  BufferedReader br = new BufferedReader(new InputStreamReader(ds));
			  String Text; 
			  int i=0;
			  while ((Text = br.readLine()) != null)   {
				  
				  Log.e("NT","Text="+i+"="+Text);
				  
				  i++;
			  }  
			  ds.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

		
	
	

}
